package five_row;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Jieshou extends HttpServlet {
	//public static GoBang gobang;
	public static five_row Five_row;
	public Jieshou() {
		super();
	}

	
	public void destroy() {
		super.destroy(); 
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	
	@SuppressWarnings("static-access")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String k=request.getParameter("k");
		boolean K = k.equals("true") ? true : false;
		if(K){
			//调用悔棋方法
			Huiqi hq=new Huiqi();
			int i=Five_row.index;
			// Five_row.arr_2[Five_row.index];
			hq.peek();
			System.out.println("1 "+Five_row.arr_1[Five_row.index]+" "+Five_row.arr_2[Five_row.index]);
			//System.out.println(Five_row.index);
			//System.out.println(i+" "+j);
			//转化为json
			//String json = "{\"i\":\""+Five_row.arr_1[Five_row.index]+"\"}";
			String json="{}";
			JSONObject jsonObj = JSONObject.fromObject(json);
			jsonObj.put("j",Five_row.arr_2[Five_row.index]);
			jsonObj.put("i",Five_row.arr_1[Five_row.index]);
			System.out.println(jsonObj.toString());
			out.write(jsonObj.toString());
			System.out.print("传值成功----");
			
		}
		if(!K){
		
		int i = Integer.parseInt(request.getParameter("i"));
		int j = Integer.parseInt(request.getParameter("j"));
		System.out.println("i--"+i);
		String h=request.getParameter("h");
		boolean H = h.equals("true") ? true : false;
		
		Five_row=new five_row();
		
		//gobang=new GoBang();
		int g=Five_row.chuancan(H,i,j);
		if(g==1){
			System.out.println("----黑棋赢-------");
			out.print("黑棋赢了");
		}
		else if(g==2){
			System.out.println("----白棋赢-------");
			out.print("白棋赢了");
		}
	}

	}
	public void init() throws ServletException {
		

}
}